﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLLottery.LotterySrv;
using SLLottery.Common;

namespace SLLottery.Controls
{
    public partial class RadioButtonPanel : UserControl
    {
        public RadioButtonPanel()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(RadioButtonPanel_Loaded);
        }
        public string Text
        {
            get;
            set;
        }
        public string Value
        {
            get;
            set;
        }
        public int ChoosedTag
        {
            get;
            set;
        }
        public int ChoosedIndex = 0;
        public bool isCreated = false;
        public void CreateElement()
        {
            MainPage.dicWs.Clear();
            string[] array = Text.Split(',');
            string[] values =Value.Split(',');
            for (int i = 0; i < array.Length; i++)
            {
                RadioButton rb = new RadioButton();
                rb.Content = array[i];
                if (i == ChoosedIndex)
                {
                    rb.Foreground = new SolidColorBrush(Common.MyColor.ConvertColor("#ff2ADC16"));
                    rb.IsChecked = true;
                }
                else
                    rb.Foreground = new SolidColorBrush(Colors.White);
                if (values.Length >= i)
                {
                    rb.Tag = values[i];
                }
                else
                    rb.Tag = i;
                rb.Style = App.Current.Resources["RadioButtonStyle1"] as Style;
                rb.Checked += (s, er) =>
                {
                    rb.Foreground = new SolidColorBrush(Common.MyColor.ConvertColor("#ff2ADC16"));
                    RadioButton nowButton = (RadioButton)s;
                    ChoosedTag = Convert.ToInt32(nowButton.Tag);
                    List<PlayTypeRadioInfo> list = App.Session[Constant.PLAYTYPERADIO_SESSION] as List<PlayTypeRadioInfo>;
                    PlayTypeRadioInfo ptri = list.Where(p => p.PlayTypeRadioID == ChoosedTag).FirstOrDefault();
                    Constant.Multiple = (int)ptri.Multiple;
                    Constant.BaseScale = ptri.BaseScale;
                    Constant.MaxScale = BonusCalc.GetMaxScale();
                    Constant.MinScale = BonusCalc.GetMinScale();
                    Constant.MaxBonusPct = BonusCalc.GetMaxBonusPct();
                    Constant.BaseMinScale = ptri.MinScale;
                    MainPage.playTypeRadio = ChoosedTag;
                    MainPage.TouzhuControl.SetBonus();
                    foreach (RadioButton radio in this.PlayTypePanel.Children)
                    {
                        if (!radio.Equals(nowButton))
                        {
                            radio.IsChecked = false;
                            radio.Foreground = new SolidColorBrush(Colors.White);
                        }
                    }
                };
                this.PlayTypePanel.Children.Add(rb);
            }
            isCreated = true;
        }
        void RadioButtonPanel_Loaded(object sender, RoutedEventArgs e)
        {
            //if (!isCreated)
            //    CreateElement();
        }
        public string GetChoosedType()
        {
            foreach (RadioButton rb in PlayTypePanel.Children)
            {
                if ((bool)rb.IsChecked)
                    return rb.Content.ToString();
            }
            return string.Empty;
        }
        public RadioButton GetChoosedRadioButton()
        {
            foreach (RadioButton rb in PlayTypePanel.Children)
            {
                if ((bool)rb.IsChecked)
                    return rb;
            }
            return null;
        }
        public List<RadioButton> GetRadioButton()
        {
            List<RadioButton> list = new List<RadioButton>();
            foreach (RadioButton rb in PlayTypePanel.Children)
            {
                list.Add(rb);
            }
            return list;
        }
    }
}
